/** Author: David Powell
This file produces Figure D1 and a dta file with the synthetic controls weights (Table D2)

This file requires the _gwtmean package
**/


clear all
set more off
set mat 800
set seed 8721

global dir "/jules/b/dpowell"
global DATA "${dir}/purdue/replication/DATA"
global OUTPUT "${dir}/purdue/replication/output"


use mortalitydata_qtr

gen overdose_rate=100000*(overdose/totpop)
gen opioid_rate=100000*opioids/totpop

**create quarterly variables
gen str tmp=string(year)
gen str tmp2=string(quarter)
replace tmp=tmp+"-"+tmp2
gen dt=quarterly(tmp, "YQ", 2050)
format %tqCY dt
drop tmp*
egen ggg=group(year quarter)
keep if ggg<.

sum ggg if year==1996
local first=r(min)
local treat_y2=`first'-1

tsset stf ggg


preserve
local res= ""
foreach jjj of numlist 1/`treat_y2' {
	local res  `"`res'   overdose_rate(`jjj')"'
}



gen pred=.
foreach nnn of numlist 6 16/17 36 48 {


	levelsof stf if nontripl==1, local(donors)
	synth overdose_rate `res', trunit(`nnn') trperiod(`first') counit(`donors') 
	matrix A2=e(Y_synthetic)
	matrix B2=e(Y_treated)
	matrix W=e(W_weights)
	svmat A2
	svmat B2

	foreach nnn2 of numlist 1/140 {
		gen tmp=A2 if _n==`nnn2'
		egen tmp2=max(tmp)
		replace pred=tmp2 if stf==`nnn' & ggg==`nnn2'
		drop tmp*
	}
	drop A* B*
	if (`nnn'==6) {
		matrix WW=W
	}
	else {
		matrix WW=(WW, W)
	}

}


keep if nontripl==0
egen pred1=mean(pred), by(ggg)
egen pred_weighted=wtmean(pred), by(ggg) weight(totpop)

egen true1=mean(overdose_rate), by(ggg)
egen true_weighted=wtmean(overdose_rate), by(ggg) weight(totpop)


bys ggg: keep if _n==1



# delimit ;


twoway  (connected true1 dt if nontripl==0,  lpattern(dash)  lwidth(thick) msize(vtiny) sort yaxis(1))  (connected pred1 dt,   lwidth(thick) msize(vtiny) sort yaxis(1))
, xline(144) graphregion(color(white))  ylabel(,nogrid) xlabel(92(8)231, angle(45))  legend(label(1 "Triplicate States") label(2 "Synthetic Triplicate States") rows(1) ) xtitle("Year") ytitle("Overdose Deaths per 100,000");
gr export ${OUTPUT}/figureD1A.eps, replace;


twoway  (connected true_weighted dt if nontripl==0,  lpattern(dash)  lwidth(thick) msize(vtiny) sort yaxis(1))  (connected pred_weighted dt,   lwidth(thick) msize(vtiny) sort yaxis(1))
, xline(144) graphregion(color(white))  ylabel(,nogrid) xlabel(92(8)231, angle(45))  legend(label(1 "Triplicate States") label(2 "Synthetic Triplicate States") rows(1) ) xtitle("Year") ytitle("Overdose Deaths per 100,000");
gr export ${OUTPUT}/figureD1B.eps, replace;



# delimit cr


restore


preserve
local res= ""
foreach jjj of numlist 1/`treat_y2' {
	local res  `"`res'   opioid_rate(`jjj')"'
}



gen pred=.
foreach nnn of numlist 6 16/17 36 48 {


	levelsof stf if nontripl==1, local(donors)
	synth opioid_rate `res', trunit(`nnn') trperiod(`first') counit(`donors') 
	matrix A2=e(Y_synthetic)
	matrix B2=e(Y_treated)
	matrix W=e(W_weights)
	svmat A2
	svmat B2

	foreach nnn2 of numlist 1/140 {
		gen tmp=A2 if _n==`nnn2'
		egen tmp2=max(tmp)
		replace pred=tmp2 if stf==`nnn' & ggg==`nnn2'
		drop tmp*
	}
	drop A* B*
	matrix WW=(WW, W)

}


keep if nontripl==0
egen pred1=mean(pred), by(ggg)
egen pred_weighted=wtmean(pred), by(ggg) weight(totpop)

egen true1=mean(opioid_rate), by(ggg)
egen true_weighted=wtmean(opioid_rate), by(ggg) weight(totpop)


bys ggg: keep if _n==1



# delimit ;


twoway  (connected true1 dt if nontripl==0,  lpattern(dash)  lwidth(thick) msize(vtiny) sort yaxis(1))  (connected pred1 dt,   lwidth(thick) msize(vtiny) sort yaxis(1))
, xline(144) graphregion(color(white))  ylabel(,nogrid) xlabel(92(8)231, angle(45))  legend(label(1 "Triplicate States") label(2 "Synthetic Triplicate States") rows(1) ) xtitle("Year") ytitle("Overdose Deaths per 100,000");
gr export ${OUTPUT}/figureD1C.eps, replace;


twoway  (connected true_weighted dt if nontripl==0,  lpattern(dash)  lwidth(thick) msize(vtiny) sort yaxis(1))  (connected pred_weighted dt,   lwidth(thick) msize(vtiny) sort yaxis(1))
, xline(144) graphregion(color(white))  ylabel(,nogrid) xlabel(92(8)231, angle(45))  legend(label(1 "Triplicate States") label(2 "Synthetic Triplicate States") rows(1) ) xtitle("Year") ytitle("Overdose Deaths per 100,000");
gr export ${OUTPUT}/figureD1D.eps, replace;



# delimit cr


restore



clear
svmat WW
rename WW1 stfips
statastates, fips(stfips)
drop _m WW3 WW5 WW7 WW9 WW11 WW13 WW15 WW17 WW19
order state_name
save ${OUTPUT}/synthweights, replace


